package hl.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import hl.pageModel.DataGrid;
import hl.pageModel.Json;
import hl.pageModel.Menu;
import hl.service.MenuService;

@Namespace("/")
@Action(value="menuAction")
public class MenuAction extends BaseAction implements ModelDriven<Menu>{

	Menu menu = new Menu();
	
	private MenuService menuService; 
	
	@Override
	public Menu getModel() {
		return menu;
	}

	public MenuService getMenuservice() {
		return menuService;
	}
	
	@Autowired
	public void setMenuservice(MenuService menuService) {
		this.menuService = menuService;
	}
	
	public void getTreeNode() {
		super.writeJson(menuService.getTreeNode(menu.getId()));
	}
	
	public void getDatagrid() {
		DataGrid d= menuService.getDatagrid(menu);
		super.writeJson(d);
	}
	
	public void add() {
		Json j=new Json();
		try {
			menuService.save(menu);
			j.setSuccess(true);
			j.setMsg("添加成功！");
	
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
	
	public void getMenuCombox() {
		super.writeJson(menuService.getMenuCombox());
	}
	
	public void edit() {
		Json j=new Json();
		try {
			menuService.edit(menu);
			j.setSuccess(true);
			j.setMsg("修改成功！");
	
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
	
	public void remove() {
		Json j=new Json();
		try {
			menuService.remove(menu);
			j.setSuccess(true);
			j.setMsg("删除成功！");
	
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}
}
